/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package POS;

//import javax.ejb.Singleton;

import java.io.*;

import org.w3c.dom.*;

import javax.xml.parsers.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;


//@Singleton
public class MessageStorageBean {

    private String from = "[ unknown ]";
    private String to = "[ me ]";
    private String date;
    private String subject = "[ no subject ]";
    private String body = "[ message body is empty ]";


    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getFrom() {
        return from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getTo() {
        return to;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void saveMessage(String xmlFileName, boolean saveToSent)
    {
        try {
            /////////////////////////////
            //Creating an empty XML Document

            //We need a Document
            DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
            Document doc = docBuilder.newDocument();

            ////////////////////////
            //Creating the XML tree

            //create the root element and add it to the document
            Element messageElement = doc.createElement("message");
            doc.appendChild(messageElement);

            //create a comment and put it in the root element
//            Comment comment = doc.createComment("Just a thought");
//            root.appendChild(comment);

            //create child element, add an attribute, and add to root
//            Element child = doc.createElement("child");
//            child.setAttribute("name", "value");
//            root.appendChild(child);

            Element fromElement = doc.createElement("from");
            messageElement.appendChild(fromElement);
            fromElement.appendChild(doc.createTextNode(this.from));

            Element toElement = doc.createElement("to");
            messageElement.appendChild(toElement);
            toElement.appendChild(doc.createTextNode(this.to));

            Element dateElement = doc.createElement("date");
            messageElement.appendChild(dateElement);
            dateElement.appendChild(doc.createTextNode(this.date.toString()));

            Element subjectElement = doc.createElement("subject");
            messageElement.appendChild(subjectElement);
            subjectElement.appendChild(doc.createTextNode(this.subject));

            Element bodyElement = doc.createElement("body");
            messageElement.appendChild(bodyElement);
            bodyElement.appendChild(doc.createTextNode(this.body));

            /////////////////
            //Output the XML

            //set up a transformer
            TransformerFactory transfac = TransformerFactory.newInstance();
            Transformer trans = transfac.newTransformer();
            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            trans.setOutputProperty(OutputKeys.INDENT, "yes");

            //create string from xml tree
            StringWriter sw = new StringWriter();
            StreamResult result = new StreamResult(sw);
            DOMSource source = new DOMSource(doc);
            trans.transform(source, result);
            String xmlString = "<?xml version=\"1.0\"?>\n";
            xmlString += sw.toString();

            File recipientDir = new File("C:\\POS\\" + this.to + "\\inbox");

            // Check if the folder exists
            if (!recipientDir.exists())
            {
                recipientDir.mkdir();
            }

            BufferedWriter out = new BufferedWriter(
                new FileWriter(recipientDir.getAbsolutePath() + "\\" + xmlFileName)
                );
            out.write(xmlString);
            out.close();

            if (saveToSent)
            {
                // Save to the Sent folder of the sender
                File senderDir = new File("C:\\POS\\" + this.from + "\\sent");

                // Check if the folder exists
                if (!senderDir.exists())
                {
                    senderDir.mkdir();
                }

                out = new BufferedWriter(
                    new FileWriter(senderDir.getAbsolutePath() + "\\" + xmlFileName)
                    );
                out.write(xmlString);
                out.close();
            }

        } catch (Exception e) {
            System.out.println(e);
        }

    }

    

    
 
}
